<template>
	<!-- 新增代码内容是比较多的,建议抽取出来,定义到一个独立的vue文件中
	    在列表组件中导入添加组件
	 -->
	<el-dialog title="新增学生" :visible.sync="dialogVisible" width="50%">
		<el-form ref="form" label-width="80px">
			<el-form-item required="true" label="学号">
				<el-input v-model="form.account"></el-input>
			</el-form-item>
			<el-form-item required="true" label="姓名">
				<el-input v-model="form.name"></el-input>
			</el-form-item>
			<el-form-item required="true" label="性别">
				<el-radio-group v-model="form.sex">
					<el-radio label="男"></el-radio>
					<el-radio label="女"></el-radio>
				</el-radio-group>
			</el-form-item>
<!--        新增自我介绍 提示字数100字-->
      <el-form-item required="true" label="自我介绍">
        <el-input type="textarea" v-model="form.info"></el-input>
      </el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="dialogVisible = false">取 消</el-button>
			<el-button type="primary" @click="save()">保 存</el-button>
		</span>
	</el-dialog>
</template>

<script>
import { data } from 'autoprefixer'
import { formToJSON } from 'axios';
	export default {
		data() {
			return {
				dialogVisible: false,
				form: {
          account:"",
					name: "",
					sex: "",
          info: "",
				}
			}
		},
		methods: {
			save(){
        if (this.form.account == "" || this.form.name == "" || this.form.sex == "" || this.form.info == ""){
            this.$message.error("请填写完整信息");
            return;
        }
        if (this.form.account.length >6){
            this.$message.error("学号长度不能超过6位");
            return;
        }
        // 学号必须是数字
        if (isNaN(this.form.account)){
            this.$message.error("学号必须是数字");
            return;
        }
				this.$http.post("api/admin",jsonToString(this.form)).then((resp)=>{
					if(resp.data.code == 200){
						dialogVisible:false,
						this.$router.go();//更新当前路由路件
					}else{
            this.$message.error("学号存在或者不符合规定");
            return;
          }
				})
			}
		}
		
	}
	//将json对象序列化为 键=值&键=值
	function jsonToString(form){
		   var str = "";
		   for(var s in form){
				str+=s+"="+form[s]+"&";	  
		   }
		   return str.substring(0,str.length-1);
	   }
</script>

<style>
</style>